package com.yiya.ms.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.model.InventoryCplex;
import com.yiya.model.InventoryCplexModel;
import com.yiya.model.InventoryStatisticsModel;
import com.yiya.service.InventoryService;
import com.yiya.utils.DataTypeUtil;
import com.yiya.utils.HtmlUtil;

@Controller
@RequestMapping("/inventory")
public class InventoryAction extends BaseAction {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(InventoryAction.class);
	
	@Autowired
	private InventoryService inventoryService;
	
	@RequestMapping("/product_list")
	public ModelAndView productList(HttpServletRequest request) {
		Map<String, Object> context = getRootMap();
		return forword("inventory/inventory", context);
	}
	
	@RequestMapping("/product_simple_list")
	public ModelAndView productSimpleList(HttpServletRequest request) {
		Map<String, Object> context = getRootMap();
		return forword("inventory/inventory_simple", context);
	}
	
	@RequestMapping("/material_list")
	public ModelAndView materialList(HttpServletRequest request) {
		Map<String, Object> context = getRootMap();
		return forword("inventory/rm_inventory", context);
	}
	
	@RequestMapping("/dataList")
	public void datalist(InventoryCplexModel model, HttpServletResponse response)
			throws Exception {
		List<InventoryCplex> inventorys = inventoryService.queryByList(model);
		int count = inventoryService.queryByCount(model);
		
		List<Map<String, Object>> footer = new ArrayList<Map<String, Object>>();
		
		//获取合计数据
		InventoryStatisticsModel statisticsModel = inventoryService.queryFooting(model);
		
		Float priceSum = 0f;
		int quantitySum = 0;
		Float amountSum = 0f;
		
		if(statisticsModel!=null){
			priceSum = statisticsModel.getPrice()==null?0:statisticsModel.getPrice();
			quantitySum = statisticsModel.getQuantity()==null?0:statisticsModel.getQuantity();
			amountSum = statisticsModel.getAmount()==null?0:statisticsModel.getAmount();
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("p.code", "合计：");
		map.put("price", DataTypeUtil.KeepDecimal(2, priceSum));
		map.put("quantity", quantitySum);
		map.put("amount", DataTypeUtil.KeepDecimal(2, amountSum));
		footer.add(map);
		
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("rows", inventorys);
		jsonMap.put("total", count);
		jsonMap.put("footer", footer);
		HtmlUtil.writerJson(response, jsonMap);
	}


}
